package com.mai4j.maiadmin.setup;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.io.IOUtils;
import org.apache.commons.text.StringSubstitutor;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

/**
 * 环境变量
 */
@Setter
@Getter
public class Env {

    /**
     * 数据库配置
     */
    @Setter
    @Getter
    @Accessors(chain = true)
    public static class Db {
        /**
         * DB URL
         */
        private String url;
        /**
         * 数据库
         */
        private String database;
        /**
         * 参数
         */
        private String query;
        /**
         * 数据库用户名
         */
        private String username;
        /**
         * 密码
         */
        private String password;
        /**
         * 数据库方言
         */
        private String dialect;
        /**
         * 表前缀
         */
        private String prefix;
    }

    /**
     * 数据库配置
     */
    @Setter
    @Getter
    @Accessors(chain = true)
    public static class Author {
        /**
         * 网站
         */
        private String website;
        /**
         * 邮箱
         */
        private String email;
        /**
         * 姓名
         */
        private String name;
    }

    private static final Env env = new Env();

    /**
     * 配置信息
     */
    private Properties properties;
    /**
     * 数据库配置
     */
    private Db db;
    /**
     * 作者信息
     */
    private Author author;

    private Env() {
    }

    /**
     * 获取Env
     *
     * @return Env
     */
    public static Env get() {
        return env;
    }

    /**
     * 读取DB的配置
     *
     * @return Db
     */
    public static Db db() {
        return env.getDb();
    }


    /**
     * 读取作者配置
     *
     * @return Author
     */
    public static Author author() {
        return env.getAuthor();
    }


    /**
     * 读取所有的配置
     *
     * @return Properties
     */
    public static Properties properties() {
        return env.getProperties();
    }

    /**
     * 设置环境变量
     *
     * @param content content
     */
    static synchronized void setupEnv(String content) {
        StringSubstitutor sub = new StringSubstitutor(System.getenv());
        sub.setValueDelimiter(":");
        try (InputStream in = IOUtils.toInputStream(sub.replace(content), StandardCharsets.UTF_8)) {
            Properties properties = new Properties();
            properties.load(in);
            env.setProperties(properties);
            env.setDb(new Db().setUrl(properties.getProperty("db.url"))
                    .setDatabase(properties.getProperty("db.database"))
                    .setQuery(properties.getProperty("db.query"))
                    .setUsername(properties.getProperty("db.username"))
                    .setPassword(properties.getProperty("db.password"))
                    .setDialect(properties.getProperty("db.dialect"))
                    .setPrefix(properties.getProperty("db.prefix"))
            );
            env.setAuthor(new Author().setWebsite(properties.getProperty("author.website"))
                    .setName(properties.getProperty("author.name"))
                    .setEmail(properties.getProperty("author.email")));
        } catch (IOException e) {
            throw new RuntimeException("error parsing properties.", e);
        }
    }

}
